<?php

namespace app\models;

use think\Model;

/**
 * Class CampusModel.
 */
class GoodsModel extends Model
{
    protected $table = 'edu_goods';

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *
     * @return string
     */
    public static function getIds()
    {
        $campus = self::select()->toArray();
        $arr = [];
        foreach ($campus as $cap) {
            $arr[] = $cap['id'];
        }

        return implode(',', $arr);
    }

    /**
     * @param $id
     * @param string $type
     *
     * @throws \think\exception\DbException
     *
     * @return null|array|AuthGroupAccessModel
     */
    public static function getById($id, $type = 'all')
    {
        if (!is_numeric($id)) {
            return [];
        }
        $campus = self::get(['id' => $id]);
        if ($campus) {
            return (isset($campus[$type])) ? $campus[$type] : $campus;
        }

        return [];
    }

    /**
     * @param $param
     *
     * @throws \think\exception\DbException
     *
     * @return array
     */
    public function getGoodsLists($param)
    {
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = [];
        $where['status'] = 1; //未被禁用
        if (isset($param['goods_name']) && '' !== $param['goods_name']) {
            $where['goods_name'] = ['like', '%'.$param['goods_name'].'%'];
        }

        if (isset($param['campus_id']) && '' !== $param['campus_id']) {
            $where['campus_id'] = $param['campus_id'];
        }else{
            if (get_current_type() == 4 || get_current_type() == 5) {
                $where['campus_id'] = get_current_campus_id();
            } elseif (get_current_type() == 2 || get_current_type() == 3) {
                $where['campus_id'] = ['in', get_current_campus_id()];
            }
        }


        if (isset($param['goods_checkbox_value']) && '' !== $param['goods_checkbox_value']) {
            if($param['goods_checkbox_value'] == 1){  //显示有存量的物品
                $where['residue_num'] = array('gt',0);
            }
        }

        if (isset($param['type_goods']) && '' !== $param['type_goods']) {
            $where['type'] = $param['type_goods'];
        }



        if(get_current_type() != 1){
			$where['company_id'] = get_current_company_id();
		}
        $listData = self::where($where)
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();
        if (!empty($listData)) {
            foreach ($listData['data'] as &$item) {
				$item['campus_name'] = CampusModel::get($item['campus_id'])['campus_name'];
                $item['price'] =  ($item['price'] / 100);
                $item['cost_price'] = ($item['cost_price'] / 100);
                if($item['type'] == 0){
                    $item['type_text'] = '物品类';
                }else if($item['type'] == 1){
                    $item['type_text'] = '杂费类';
                    $item['put_nums'] = '-';
                    $item['cost_price'] = '-';
                    $item['residue_num'] = '-';
                }
            }
        }

        return $listData;
    }


}
